package com.dzsw.mybatisx.generate;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.dzsw.mybatisx.generate.util.GenConf;
import com.dzsw.mybatisx.generate.util.MyTemplateEngine;
import com.dzsw.mybatisx.generate.util.YamlUtil;

public class MysqlGenerator {

    public static void main(String[] args) throws Exception {
        GenConf conf = YamlUtil.readGenConfYaml();
        generate(conf, "biz_ys_offline_reg", "biz_ys_offline_reg_result");
    }

    public static void generate(GenConf conf, String... tableNames) {
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(conf.getOutputPath());
        globalConfig.setAuthor(conf.getAuthor());
        globalConfig.setEnableCache(false);// XML 二级缓存
        globalConfig.setBaseResultMap(true);// XML ResultMap
        globalConfig.setBaseColumnList(true);// XML columList
        globalConfig.setOpen(false);
        globalConfig.setFileOverride(true);
//        globalConfig.setServiceName("%sService");// 生成接口前面就不会有I
        mpg.setGlobalConfig(globalConfig);
        // 数据源
        DataSourceConfig dsConfig = new DataSourceConfig();
        dsConfig.setUrl(conf.getUrl());
        dsConfig.setDriverName(conf.getDriverName());
        dsConfig.setUsername(conf.getUsername());
        dsConfig.setPassword(conf.getPwd());
        mpg.setDataSource(dsConfig);
        // 包配置
        PackageConfig pkgConf = new PackageConfig();
        pkgConf.setModuleName(conf.getModuleName());
        pkgConf.setParent(conf.getPkgParent());
        mpg.setPackageInfo(pkgConf);
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//        strategy.setSuperControllerClass(conf.superControllerClass);
//        strategy.setSuperEntityClass(conf.superEntityClass);
        strategy.setSuperMapperClass(conf.getSuperMapperClass());
        strategy.setRestControllerStyle(true);
        strategy.setEntityLombokModel(true);

        strategy.setEntityTableFieldAnnotationEnable(true);
        strategy.setInclude(tableNames);
        // 模板配置
        TemplateConfig templateConf = new TemplateConfig();
        templateConf.setEntity("templates/entity.java.ftl");
        templateConf.setMapper("templates/mapper.java.ftl");
        templateConf.setXml("templates/mapper.xml.ftl");
        templateConf.setServiceImpl("templates/service.java.ftl");
        templateConf.setController("templates/controller.java.ftl");
        // 配置生成策略和模板引擎
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new MyTemplateEngine());
        // 生成
        mpg.execute();
    }


}


